package com.groupbuying.settleproducer.service.impl;

import com.groupbuying.settleproducer.connector.YSFeign;
import com.groupbuying.settleproducer.constants.SettleUshopYsSplitSyncreceiptEnumConstants;
import com.groupbuying.settleproducer.utils.DateUtils;
import com.groupbuying.settleproducer.utils.JsonUtils;
import com.groupbuying.settleproducer.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.groupbuying.settleproducer.dao.SettleUshopYsSplitSyncreceiptDao;
import com.groupbuying.settleproducer.domain.SettleUshopYsSplitSyncreceiptDO;
import com.groupbuying.settleproducer.service.SettleUshopYsSplitSyncreceiptService;


@Service
public class SettleUshopYsSplitSyncreceiptServiceImpl implements SettleUshopYsSplitSyncreceiptService {
    public static final Logger logger = LoggerFactory.getLogger(SettleUshopYsSplitSyncreceiptServiceImpl.class);

    @Autowired
    private SettleUshopYsSplitSyncreceiptDao settleUshopYsSplitSyncreceiptDao;
    @Autowired
    private YSFeign ysFeign;

    /**
     * 分账订单异常处理
     *
     * @return
     */
    @Override
    public ApiResult ysSplitExce() {
        /*查询异常订单*/
        Map<String, Object> condMap = new HashMap<>();
        condMap.put("startTime", DateUtils.getTodayZeroTime());
        condMap.put("endTime", DateUtils.getendTime());
        condMap.put("status", SettleUshopYsSplitSyncreceiptEnumConstants.ENUM_STATUS_FAIL);
        List splitList = new ArrayList();
        try {
            List<SettleUshopYsSplitSyncreceiptDO> list = settleUshopYsSplitSyncreceiptDao.list(condMap);
            logger.info("SettleUshopYsSplitSyncreceiptServiceImpl.ysSplitExce()---查询到的异常单子是:" + list.size());
            if (list != null && list.size() > 0) {
                for (SettleUshopYsSplitSyncreceiptDO settleUshopYsSplitSyncreceiptDO : list) {
                    String note = settleUshopYsSplitSyncreceiptDO.getNote();
                    String outTradeNo = settleUshopYsSplitSyncreceiptDO.getOutTradeNo();
                    logger.info("SettleUshopYsSplitSyncreceiptServiceImpl.ysSplitExce()---获取到的异常单子的单号是:" + outTradeNo + "----该笔订单的是分账信息为:" + note);
                    Map<String, Object> map = new ConcurrentHashMap<String, Object>();
                    map.put("biz_content", note);
                    String splitReceipt = this.ysFeign.ysSplitReceipt(map);
                    //String splitReceipt = split(map);
                    logger.info("SettleUshopYsSplitSyncreceiptServiceImpl.ysSplitExce()---调用银盛分账返回值信息为:" + splitReceipt);
                    Map returnMap = JsonUtils.jsonToMap(splitReceipt);
                    logger.info("SettleUshopYsSplitSyncreceiptServiceImpl.ysSplitExce()---调用银盛分账返回值信息为:" + returnMap);
                    Boolean retuenCodeStatus = returnMap.containsKey("returnCode");
                    if (retuenCodeStatus) {
                        String returnCode = (String) returnMap.get("returnCode");
                        logger.info("SettleUshopYsSplitSyncreceiptServiceImpl.ysSplitExce()---调用银盛分账返回值信息returnCode为:" + returnCode);
                        if (returnCode.equalsIgnoreCase(SettleUshopYsSplitSyncreceiptEnumConstants.ENUM_RETURN_CODE)) {
                            settleUshopYsSplitSyncreceiptDO.setReturnCode((String) returnMap.get("returnCode"));
                            settleUshopYsSplitSyncreceiptDO.setRetrunInfo((String) returnMap.get("retrunInfo"));
                            settleUshopYsSplitSyncreceiptDO.setStatus(SettleUshopYsSplitSyncreceiptEnumConstants.ENUM_STATUS_SUCCESS);
                            settleUshopYsSplitSyncreceiptDO.setMsg((String) returnMap.get("msg"));
                            settleUshopYsSplitSyncreceiptDO.setCode((String) returnMap.get("code"));
                            settleUshopYsSplitSyncreceiptDO.setOutTradeNo(outTradeNo);
                            settleUshopYsSplitSyncreceiptDO.setNote(SettleUshopYsSplitSyncreceiptEnumConstants.ENUM_END_NOTE);
                            settleUshopYsSplitSyncreceiptDO.setGmtModified(new Date());
                            settleUshopYsSplitSyncreceiptDao.update(settleUshopYsSplitSyncreceiptDO);
                            logger.info("SettleUshopYsSplitSyncreceiptServiceImpl.ysSplitExce()---异常订单调用银盛分账成功，更改数据库成功。对应的单号是:" + outTradeNo);
                            splitList.add("单号为:" + outTradeNo + "----成功返回信息:" + splitReceipt);
                        }
                    } else {
                        splitList.add("单号为:" +outTradeNo + "----异常返回信息:" + splitReceipt);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("SettleUshopYsSplitSyncreceiptServiceImpl.ysSplitExce()---异常信息:" + e.getMessage());
        }
        if (splitList.size() > 0) {
            return ApiResult.success(splitList);
        } else {
            return ApiResult.success("没有异常的单子");
        }

    }

    private String split(Map<String, Object> map) {
        String json = "{\n" +
                "    \"code\":\"10000\",\n" +
                "    \"msg\":\"Success\",\n" +
                "    \"returnCode\":\"0000\",\n" +
                "    \"retrunInfo\":\"受理成功!\"\n" +
                "}";
        return json;

    }
}
